<?php
namespace common\modules\bookmark;

class ModuleInfo extends \common\core\modularity\ModuleInfo
{
    public function init()
    {
        parent::init();

        $this->id = 'bookmark';
        $this->name = '书签';
        $this->description = '书签模块';
        $this->version = '1.0.0';
        $this->icon = '<i class="fa fa-bookmark"></i>';
        $this->has_backend = false;
        $this->has_frontend = false;
        $this->has_api = false;

        $this->is_system = false;

        $this->requireModules = 'user';
    }

    public function install()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%bookmark}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer(),
            'type' => $this->smallInteger(2)
                ->notNull(),
            'name' => $this->string(50)
                ->notNull(),
            'object_count' => $this->integer()
                ->defaultValue(0),
            'status' => $this->smallInteger()
                ->notNull()
                ->defaultValue(0),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer()
        ], $tableOptions);

        $this->createTable('{{%bookmark_item}}', [
            'id' => $this->primaryKey(),
            'bookmark_id' => $this->integer()
                ->notNull(),
            'object_id' => $this->integer()
                ->notNull(),
            'created_at' => $this->integer()
        ], $tableOptions);

        $this->addForeignKey('fk_bookmark_user', '{{%bookmark}}', 'user_id', '{{%user}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_bookmark_item_favorite', '{{%bookmark_item}}', 'bookmark_id', '{{%bookmark}}', 'id', 'cascade', 'cascade');
    }

    public function uninstall()
    {
        $this->dropForeignKey('fk_favorite_item_favorite', '{{%favorite_item}}');
        $this->dropForeignKey('fk_favorite_user', '{{%favorite}}');
        $this->dropTable('{{%favorite_item}}');
        $this->dropTable('{{%favorite}}');
    }
}